package com.power.services.funding;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.funding.RzMoneysideDAO;
import com.power.model.funding.RzMoneyside;
import com.power.util.PageUtil;

@Service("rzMoneysideService")
public class RzMoneysideServiceImpl implements RzMoneysideService {

	@Autowired
	private RzMoneysideDAO dao;

	public void create(RzMoneyside arg0) {
		dao.save(arg0);
	}

	public void delete(RzMoneyside arg0) {
		dao.delete(arg0);
	}

	public RzMoneyside read(Integer arg0) {
		return dao.get(arg0);
	}

	public void update(RzMoneyside arg0) {
		dao.update(arg0);
	}

	public void saveOrUpdate(RzMoneyside obj) {
		dao.saveOrUpdate(obj);		
	}

	public PageUtil showFunding(String keyword, int pageNo, int limit) {
		PageUtil pu = new PageUtil();
		DetachedCriteria criteria = DetachedCriteria.forClass(RzMoneyside.class);
		if(!"".equals(keyword)){
		criteria.add(Restrictions.eq("investmentType", keyword));
		}
		criteria.addOrder(Order.asc("updateTime"));
		List<RzMoneyside> list = dao.findByCriteria(criteria, (pageNo-1)*limit, limit);
		pu.setList(list);
		pu.setPageNo(pageNo);
		pu.setPageSize(limit);
		pu.setTotalRecords(dao.getRowCount(criteria));
		return pu;
	}
	
	@SuppressWarnings("unchecked")
	public List<RzMoneyside> findRecommend() {
		String hql = "select d from com.power.model.funding.RzMoneyside d   where d.recommend='0' and sourceType <>0 order by id desc  ";
		return dao.find(hql,0,2);
	}
	
	@SuppressWarnings("unchecked")
	public List<RzMoneyside> findHot() {
		String hql = "select d from com.power.model.funding.RzMoneyside d where d.isHot='0' and sourceType <>0 order by id desc ";
		return dao.find(hql,0,10);
	}


	public List<RzMoneyside> findALLbyParam(Map<String, String> map, int start,
			int limit) {
		String hql = "from com.power.model.funding.RzMoneyside";
		return dao.find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<RzMoneyside> findlist(int limit,String str) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RzMoneyside.class);
		if(str!=null){
		criteria.add(Restrictions.eq("investmentType", str));
		}
		criteria.addOrder(Order.asc("updateTime"));
		criteria.add(Restrictions.ne("sourceType", 0));
		List<RzMoneyside> list = dao.findByCriteria(criteria, 0, limit);
		return list;
	}
	
//	public void updatePassword(RzMoneyside user) {
//		dao.executeSql("UPDATE t_s_sysuser SET PASSWORD='" + user.getPassword()
//				+ "' WHERE id=" + user.getId());
//	}
//
//	@SuppressWarnings("unchecked")
//	public List<RzMoneyside> findByLogonnameAndPassword(RzMoneyside obj) {
//		return dao
//				.find("select d from com.power.model.user.RzMoneyside d  where  d.username='"
//						+ obj.getUsername()
//						+ "'"
//						+ " and d.password ='"
//						+ obj.getPassword() + "'");
//	}
}
